package com.ruoyi.system.service.equipment.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.equipment.EquipmentCheckPlan;
import com.ruoyi.system.entity.SysRemind;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.equipment.EquipmentCheckPlanMapper;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.SysRemindService;
import com.ruoyi.system.service.equipment.IEquipmentCheckPlanService;
import com.ruoyi.system.service.impl.EnterpriseWXServiceImpl;
import org.drools.core.rule.Collect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * 设备检测计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-17
 */
@Service
public class EquipmentCheckPlanServiceImpl implements IEquipmentCheckPlanService
{
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private SysRemindService sysRemindService;
    @Autowired
    private EquipmentCheckPlanMapper equipmentCheckPlanMapper;
    @Resource
    private EnterpriseWXServiceImpl enterpriseWXService;

    /**
     * 查询设备检测计划
     * 
     * @param id 设备检测计划主键
     * @return 设备检测计划
     */
    @Override
    public EquipmentCheckPlan selectEquipmentCheckPlanById(Long id)
    {
        return equipmentCheckPlanMapper.selectEquipmentCheckPlanById(id);
    }

    /**
     * 查询设备检测计划列表
     * 
     * @param equipmentCheckPlan 设备检测计划
     * @return 设备检测计划
     */
    @Override
    public List<EquipmentCheckPlan> selectEquipmentCheckPlanList(EquipmentCheckPlan equipmentCheckPlan)
    {
        if (StringUtils.isNotEmpty(equipmentCheckPlan.getIds())) {
            equipmentCheckPlan.setIdAry(Arrays.asList(equipmentCheckPlan.getIds().split(",")));
        }
        return equipmentCheckPlanMapper.selectEquipmentCheckPlanList(equipmentCheckPlan);
    }

    /**
     * 新增设备检测计划
     * 
     * @param equipmentCheckPlan 设备检测计划
     * @return 结果
     */
    @Override
    public int insertEquipmentCheckPlan(EquipmentCheckPlan equipmentCheckPlan)
    {
        equipmentCheckPlan.setCreatedDate(new Date());
        equipmentCheckPlan.setCreatedBy(SecurityUtils.getUsername());
        return equipmentCheckPlanMapper.insertEquipmentCheckPlan(equipmentCheckPlan);
    }

    /**
     * 导入设备检测计划
     *
     * @param list
     * @return 查询结果
     */
    @Override
    public String importHandler(List<EquipmentCheckPlan> list, String loginUser) {
        if (StringUtils.isEmpty(list)) {
            throw new ServiceException("导入设备数据不能为空！");
        }
        list.forEach(e -> {
            e.setCreatedBy(loginUser);
            e.setCreatedDate(new Date());
        });
        equipmentCheckPlanMapper.insertBatch(list);
        return "成功";
    }

    /**
     * 修改设备检测计划
     * 
     * @param equipmentCheckPlan 设备检测计划
     * @return 结果
     */
    @Override
    public int updateEquipmentCheckPlan(EquipmentCheckPlan equipmentCheckPlan)
    {
        equipmentCheckPlan.setModifyDate(new Date());
        equipmentCheckPlan.setModifyBy(SecurityUtils.getUsername());
        return equipmentCheckPlanMapper.updateEquipmentCheckPlan(equipmentCheckPlan);
    }

    /**
     * 批量删除设备检测计划
     * 
     * @param ids 需要删除的设备检测计划主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentCheckPlanByIds(Long[] ids)
    {
        return equipmentCheckPlanMapper.deleteEquipmentCheckPlanByIds(ids);
    }

    /**
     * 删除设备检测计划信息
     * 
     * @param id 设备检测计划主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentCheckPlanById(Long id)
    {
        return equipmentCheckPlanMapper.deleteEquipmentCheckPlanById(id);
    }



    @Scheduled(cron = "0 0 6 * * ?")
    public void checkPermitReportFile() {
        EquipmentCheckPlan plan = new EquipmentCheckPlan();
        plan.setNextRemindDate(DateUtils.parseDate(DateUtils.getDate()));
        List<EquipmentCheckPlan> planLit = equipmentCheckPlanMapper.selectEquipmentCheckPlanList(plan);
        if (CollectionUtils.isEmpty(planLit))  { return; }

        planLit.forEach(var -> {
            sendWxMsg(var.getEquipmentName(), DateUtils.parseDateToStr("yyyy-MM-dd", var.getNextCheckDate()));
        });
    }

    public void sendWxMsg(String equipmentName, String checkDate) {
        SysRole sysRole = new SysRole();
        sysRole.setRoleKey("sbjyts");
        List<SysUser> sysUsers = userMapper.selectUserByRoleName(sysRole);
        if (sysUsers.isEmpty())  { return ; }

        String message = "设备检测计划:" + equipmentName + "，即将开始，请关注！检测日期：" + checkDate;
        for (SysUser sys : sysUsers) {
            sysRemindService.insert(SysRemind.TYPE_PROMPT, message, sys.getUserId().intValue());
        }

        List<String> userWxIdList = sysUsers.stream().map(SysUser::getWxUserId).collect(Collectors.toList());
        if (StringUtils.isEmpty(userWxIdList))  { return ; }
        enterpriseWXService.sendMessageToWx(message, String.join("|", userWxIdList));
    }

}
